package com.edz.message.deal;

import com.alibaba.fastjson.JSONObject;
import com.edz.dudumifi.base.MyApplication;
import com.edz.dudumifi.event.KKTcpClientThread;
import com.edz.dudumifi.utils.HttpUtils;

import java.util.Map;
import java.util.concurrent.Callable;
import io.netty.channel.ChannelHandlerContext;

public class MessageCallable implements Callable<String> {

    private String msg;
    private ChannelHandlerContext socket;
    private String url = "http://ceshi.edawtech.com/mifi/api/";

    private String strReq, strRtn;     //请求 响应

    public MessageCallable(ChannelHandlerContext socket, String msg) {
        this.msg = msg;
        this.socket = socket;
    }

    @Override
    public String call() throws Exception {
        // TODO Auto-generated method stub
        //JSONObject json = JSON.parseObject(msg);
//        ReplyServerBody replyBody = null;
//        ReplyMsg replyMsg = new ReplyMsg();

        Map map = JSONObject.parseObject(msg, Map.class);
        String msgType = map.get("msgType").toString();

        switch (msgType) {
            //{"SignalLevel":"4","WLanMac":"38-1C-23-02-14-3D","MsgType":"GetSystemInfo","BatCharging":"True","MCCMNC":"46011","IMSI":"460111122245063","WLanStaCount":"1","NetworkTypeName":"LTE","BatCapacity":"39","IMEI1":"356793034557975","ICCID":"89860317462025227738","DataNetworkTypeClass":"3","SimSlotIndex":"0","VersionCode":"1203","IMSI2":"","WLanSSID":"4G-MFI-143D","NetType":"Connected","WLanStaList":"7c:1d:d9:77:8f:1b_0;","WLanKey":"       ","Version":"V1.1","ICCID2":"","DeviceVersion":"4G_LTE_5M_H11_C04_MV1.381","IMEI2":"356793034562975","WLanStaMaxCount":"7"}
            case "GetSystemInfo":// 系统信息
                System.out.println("\nGetSystemInfo" + msg);
                break;
            case "LbsInfo"://地理位置
                System.out.println("\nLbsInfo" + msg);
                break;
            case "HeartInfo": //心跳包
                System.out.println("\nHeartInfo" + msg);
                break;

            case "WebRedirectConfig"://配置重定向界面
                //{\"MsgType\":\"WebRedirectConfig\",\"Url\":\"https://res.v5fans.com/bigwifi/start.html?bid=2999\"}
                System.out.println("\n重定向:" + msg);
                String url = map.get("url").toString();
                String acceptIPList = map.get("acceptIPList").toString();
                strReq = "{\"MsgType\":\"WebRedirectCtl\",\"CmdType\":\"EnableRedirect\",\"WebRedirectUrl\":\"" + url + "\",\"AcceptIPList\":\""+acceptIPList+"\",\"WLanMac\":\"\",\"IPList\":\"\"}";

                KKTcpClientThread webRedirectCtl = new KKTcpClientThread(MyApplication.getContext(),strReq);
                webRedirectCtl.start();
                break;

            case "SetNetRate": //限速
                // {\"MsgType\":\"SetNetRate\",\"Rate\":\"400\"}
                System.out.println("\n限速:" + msg);
                String setNetRate = map.get("isRateLimit").toString();
                if (setNetRate.equals("n"))
                    break;

                String rate = map.get("rate").toString();
                System.out.print("\n服务器返回限速设定数据" + rate);
                strReq = "{\"MsgType\":\"SetNetRate\",\"CmdType\":\"Enable\",\"NetRate\":\"" + rate + "\"}";
                KKTcpClientThread netRate = new KKTcpClientThread(MyApplication.getContext(),strReq);
                netRate.start();
                break;

            case "OpenNetWork": //开网
                //{\"MsgType\":\"OpenNetWork\",\"Url\":\"http://192.168.199.1/goform/CommConfig?cmd=CustInvoke&action=WebRedirectCtl&item=AcceptOneTemp&subitem=34:69:87:c0:e9:45_30\"}
                System.out.println("放行上网:" + msg);
                String Url = map.get("Url").toString();
                System.out.print("\n" + Url);

                String str = HttpUtils.sendRequest(Url);
                System.out.print("get到本地局域网返回" + str);
                break;
        }

        return "success";
    }
}
